class AdminUser < ActiveRecord::Base
	def password
    @password
  end
  
	def password=(pwd)
    @password = pwd
    return if pwd.blank?
    create_new_salt
    self.crypted_password = self.class.encrypted_password(self.password, self.salt)
    
  end
  
	def self.authenticate(name,pwd)
    admin_user = self.find_by_admin_name(name)
    if admin_user
      expected_password = encrypted_password(pwd,admin_user.salt)
      if admin_user.crypted_password != expected_password
        admin_user = nil
      end
    else
      admin_user = nil
    end
    admin_user
  end
  
	private
  def self.encrypted_password(pwd,salt)
    string_to_hash = "#{pwd}@re54#4e!#{salt}"
    Digest::SHA1.hexdigest(string_to_hash)
  end

  def create_new_salt
    self.salt = Digest::SHA1.hexdigest(Time.now.to_s + rand.to_s)
  end
end
